import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.*;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

public class ZapXml2Pdf {

    Font font = null;//正常字体
    Font fontBold = null;//正常加粗字体
    Font fontBig = null;//大字体
    Font fontBigBold = null;//加粗大字体

    public  void zapXml2Pdf(String xmlFilePath,String pdfFilePath){
        try {
            SAXReader reader = new SAXReader();
            com.itextpdf.text.Document pdfDocument= new com.itextpdf.text.Document();
            PdfWriter pdfWriter = PdfWriter.getInstance(pdfDocument, new FileOutputStream(pdfFilePath));

            pdfDocument.open();

            //中文字体,解决中文不能显示问题
            BaseFont bfChinese = BaseFont.createFont("simsun.ttc,1", BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);

            font = new Font(bfChinese);//正常字体
            fontBold = new Font(bfChinese, 12, Font.BOLD);//正常加粗字体
            fontBig = new Font(bfChinese, 20);//大字体
            fontBigBold = new Font(bfChinese, 20, Font.BOLD);//加粗大字体


            Paragraph theme = new Paragraph("检测报告",fontBigBold);
            theme.setAlignment(com.itextpdf.text.Element.ALIGN_CENTER);
            pdfDocument.add(theme);

            PdfPTable table = new PdfPTable(4);
            table.setWidthPercentage(100); // 宽度100%填充
            table.setSpacingBefore(20f); // 前间距
            table.setSpacingAfter(20f); // 后间距

            List<PdfPRow> listRow = table.getRows();
            //设置列宽
            float[] columnWidths = {3f, 3f, 3f,3f};
            table.setWidths(columnWidths);

            //xml文件
            // 通过reader对象的read方法加载23234.xml文件,获取docuemnt对象。
            Document document = reader.read(new File(xmlFilePath));
            Element alertitems = document.getRootElement().element("site").element("alerts");

            Iterator it = alertitems.elementIterator();
            String alert="无",riskcode="无",riskdesc="无",desc="无",solution="无";
            // 遍历迭代器，获取根节点中的信息
            while (it.hasNext()) {

                Element alertitem = (Element) it.next();

                Iterator itt = alertitem.elementIterator();
                alert="无";
                riskcode="无";
                riskdesc="无";
                desc="无";
                solution="无";
                while (itt.hasNext()) {
                    Element alertitemChild = (Element) itt.next();
                    if(alertitemChild.getName() == "alert"){
                        alert = alertitemChild.getStringValue();
                    }
                    if(alertitemChild.getName() == "riskcode"){
                        riskcode = alertitemChild.getStringValue();
                    }
                    if(alertitemChild.getName() == "riskdesc"){
                        riskdesc = alertitemChild.getStringValue();
                    }
                    if(alertitemChild.getName() == "desc"){
                        desc = alertitemChild.getStringValue();
                    }
                    if(alertitemChild.getName() == "solution"){
                        solution = alertitemChild.getStringValue();
                    }
                }
                insertOneNode(listRow,alert,riskcode,riskdesc,desc,solution);
            }
            pdfDocument.add(table);
            pdfDocument.close();
            //关闭书写器
            pdfWriter.close();
        } catch (DocumentException | com.itextpdf.text.DocumentException | IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }




    //设置pdf表格居中
    public  void setTableCell(PdfPCell cells[]){
        for(PdfPCell cell : cells){
            // cell.setHorizontalAlignment(com.itextpdf.text.Element.ALIGN_CENTER);//水平居中
            cell.setVerticalAlignment(com.itextpdf.text.Element.ALIGN_MIDDLE);//垂直居中
        }
    }
    //插入一个alert
    public  void insertOneNode(List<PdfPRow> listRow, String... s){
        try {

            PdfPCell pcell0[] = new PdfPCell[4];
            PdfPCell pcell1[] = new PdfPCell[4];
            PdfPCell pcell2[] = new PdfPCell[4];
            PdfPCell pcell3[] = new PdfPCell[4];
            PdfPRow row0 = new PdfPRow(pcell0);
            PdfPRow row1 = new PdfPRow(pcell1);
            PdfPRow row2 = new PdfPRow(pcell2);
            PdfPRow row3 = new PdfPRow(pcell3);

            //第一行
            pcell0[0] = new PdfPCell(new Paragraph("alert", fontBold));//单元格内容
            pcell0[1] = new PdfPCell(new Paragraph(s[0], font));
            pcell0[2] = new PdfPCell(new Paragraph("riskcode", fontBold));
            pcell0[3] = new PdfPCell(new Paragraph(s[1], font));
            setTableCell(pcell0);

            //第二行
            pcell1[0] = new PdfPCell(new Paragraph("riskdesc", fontBold));//单元格内容
            pcell1[1] = new PdfPCell(new Paragraph(s[2], font));
            pcell1[2] = new PdfPCell();
            pcell1[3] = new PdfPCell();
            pcell1[1].setColspan(3);
            setTableCell(pcell1);

            //第三行
            pcell2[0] = new PdfPCell(new Paragraph("desc", fontBold));//单元格内容
            pcell2[1] = new PdfPCell(new Paragraph(s[3], font));
            pcell2[2] = new PdfPCell();
            pcell2[3] = new PdfPCell();
            pcell2[1].setColspan(3);
            setTableCell(pcell2);

            //第四行
            pcell3[0] = new PdfPCell(new Paragraph("solution", fontBold));//单元格内容
            pcell3[1] = new PdfPCell(new Paragraph(s[4], font));
            pcell3[2] = new PdfPCell();
            pcell3[3] = new PdfPCell();
            pcell3[1].setColspan(3);
            setTableCell(pcell3);


            listRow.add(row0);
            listRow.add(row1);
            listRow.add(row2);
            listRow.add(row3);
        }
        catch (Exception e){
            e.printStackTrace();
        }

    }
}
